package avicit.bdp.dgs.metadata.rest;

import avicit.bdp.core.enums.TableTypeEnum;
import avicit.bdp.dgs.metadata.dto.MetadataBaseVersionDetailDTO;
import avicit.bdp.dgs.metadata.service.MetadataBaseVersionDetailService;
import avicit.bdp.dgs.metadata.vo.MetadataBaseChangeVo;
import avicit.bdp.dgs.metadata.vo.MetadataTableVo;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.annotation.Resource;

import java_cup.version;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "元数据基线版本管理")
@RequestMapping("/api/bdp/dgs/metadata/base/version/")
public class MetadataBaseVersionRest {

  @Resource private MetadataBaseVersionDetailService metadataBaseVersionDetailService;

  @ApiOperation(value = "通过数据源ID查询基线版本变更总数")
  @ApiParam(value = "dataSourceId", name = "数据源ID", required = true)
  @GetMapping("/{dataSourceId}/updateNum")
  public ResponseMsg<Integer> getBaseVersionUpdateNum(@PathVariable String dataSourceId) {
    ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(
        metadataBaseVersionDetailService.getBaseVersionUpdateNum(dataSourceId));
    return responseMsg;
  }

  @ApiOperation(value = "分页查询基线变更元数据")
  @ApiImplicitParams({
    @ApiImplicitParam(
        value = "数据源",
        name = "dataSourceId",
        dataType = "String",
        paramType = "query",
        required = true),
    @ApiImplicitParam(value = "查询关键词", name = "keyWords", dataType = "String", paramType = "query"),
    @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int", paramType = "query"),
    @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int", paramType = "query")
  })
  @GetMapping("/page")
  public ResponseMsg<QueryRespBean<MetadataTableVo>> getUpdateDetailPageList(
      @RequestParam String dataSourceId,
      @RequestParam(required = false) String keyWords,
      @RequestParam(defaultValue = "1") Integer pageNo,
      @RequestParam(defaultValue = "10") Integer pageSize)
      throws Exception {

    ResponseMsg<QueryRespBean<MetadataTableVo>> responseMsg = new ResponseMsg<>();
    Page<MetadataTableVo> pageList =
        metadataBaseVersionDetailService.getUpdateDetailPageList(
            dataSourceId, keyWords, TableTypeEnum.TABLE_METADATA.getCode(), pageNo, pageSize);

    QueryRespBean<MetadataTableVo> queryRespBean = new QueryRespBean<>();
    queryRespBean.setResult(pageList);
    responseMsg.setResponseBody(queryRespBean);

    return responseMsg;
  }

  @ApiOperation(value = "批量变更基线版本")
  @ApiParam(value = "元数据ID数组", name = "ids", required = true)
  @PostMapping("/{dataSourceId}/batchUpdateVersion")
  public ResponseMsg<Boolean> batchUpdateVersion(
      @PathVariable String dataSourceId, @RequestBody List<String> ids) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(
        metadataBaseVersionDetailService.batchUpdateVersion(dataSourceId, ids));
    return responseMsg;
  }

  @ApiOperation(value = "变更元数据基线版本")
  @PostMapping("/change")
  public ResponseMsg<Boolean> changeBaseVersion(@RequestBody MetadataBaseChangeVo updateDto) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(metadataBaseVersionDetailService.changeBaseVersion(updateDto));
    return responseMsg;
  }
}
